<template>
	<view class="favorite-container">
		<view class="content">
			<view v-if="favoriteItems.length > 0" class="favorite-list">
				<view class="favorite-item" v-for="(item, index) in favoriteItems" :key="item.id">
					<view class="item-left" @click="goToDetail(item)">
						<image :src="item.image" class="product-image"></image>
					</view>
					<view class="item-middle">
						<view class="product-info" @click="goToDetail(item)">
							<text class="product-name">{{ item.name }}</text>
							<view class="price-section">
								<text class="product-original-price">¥{{ item.originalPrice }}</text>
								<text class="product-discount-price">¥{{ item.discountPrice }}/{{ item.unit }}</text>
							</view>
							<text class="product-sold">已售{{ item.soldCount }}件</text>
						</view>
						<view class="button-section">
							<text class="btn-unfavorite" @click="removeFromFavorite(item.id)">取消收藏</text>
							<view v-if="getItemQuantity(item.id) === 0" class="add-cart-btn" @click="addToCart(item)">加入购物车</view>
							<view v-else class="quantity-control">
								<view class="quantity-btn" @click="decreaseQuantity(item)">-</view>
								<view class="quantity-num">{{ getItemQuantity(item.id) }}</view>
								<view class="quantity-btn" @click="increaseQuantity(item)">+</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			
			<view v-else class="empty-favorite">
				<image src="/static/user/favorite.png" class="empty-image"></image>
				<text class="empty-text">还没有收藏任何商品</text>
				<button class="btn-go-shopping" @click="goShopping">去逛逛</button>
			</view>
		</view>
	</view>
</template>

<script>
	import { mapState, mapGetters, mapActions } from 'vuex'
	import auth from '@/utils/auth.js'
	
	export default {
		data() {
			return {
				// 收藏商品列表
				favoriteItems: [
					{
						id: 201,
						name: '兴宁手打猪肉丸',
						originalPrice: 5999,
						discountPrice: 4999,
						soldCount: 128,
						unit: '斤',
						image: 'https://pic.rmb.bdstatic.com/bjh/video/46cace1e2cb2827fc520fbc6769ebef5.jpeg'
					},
					{
						id: 203,
						name: '自家晒的猪肉干',
						originalPrice: 399,
						discountPrice: 299,
						soldCount: 89,
						unit: '斤',
						image: 'https://pic.rmb.bdstatic.com/bjh/241216/dump/279b49f325fde86015838cea70e4db79.jpeg'
					}
				]
			}
		},
		computed: {
			// 映射购物车相关的状态和getters
			...mapState('cart', ['cartItems']),
			...mapGetters('cart', ['getItemQuantity']),
			
			// 映射用户登录状态
			...mapGetters(['getIsLogin']),
			
			// 登录状态
			isLogin() {
				return this.getIsLogin
			}
		},
		onLoad() {
			// 初始化收藏数据
			this.loadFavoriteData()
		},
		onShow() {
			// 每次显示页面时刷新数据
			this.loadFavoriteData()
			this.initCart()
		},
		methods: {
			// 映射购物车相关的actions
			...mapActions('cart', ['addToCart', 'updateQuantity', 'removeFromCart', 'initCart']),
			
			// 加载收藏数据
			loadFavoriteData() {
				// 检查登录状态，未登录则不加载收藏数据
				if (!this.isLogin) {
					return
				}
				
				// 这里可以调用API获取收藏数据
				console.log('加载收藏数据')
			},
			
			// 跳转到商品详情
			goToDetail(item) {
				uni.navigateTo({
					url: `/pages/product/detail?id=${item.id}`
				})
			},
			
			// 从收藏中移除
			removeFromFavorite(productId) {
				uni.showModal({
					title: '提示',
					content: '确定要取消收藏这个商品吗？',
					success: (res) => {
						if (res.confirm) {
							const index = this.favoriteItems.findIndex(item => item.id === productId)
							if (index > -1) {
								this.favoriteItems.splice(index, 1)
								uni.showToast({
									title: '已取消收藏',
									icon: 'success',
									duration: 1500
								})
							}
						}
					}
				})
			},
			
			// 添加到购物车
			addToCart(item) {
				// 检查登录状态
				if (!auth.checkLoginAndRedirect(auth.getCurrentPageFullPath())) {
					return
				}
				
				this.$store.dispatch('cart/addToCart', {
					id: item.id,
					name: item.name,
					price: item.discountPrice,
					image: item.image,
					quantity: 1
				})
				uni.showToast({
					title: '已添加到购物车',
					icon: 'success'
				})
			},
			
			// 增加数量
			increaseQuantity(item) {
				// 检查登录状态
				if (!auth.checkLoginAndRedirect(auth.getCurrentPageFullPath())) {
					return
				}
				
				this.updateQuantity({
					id: item.id,
					quantity: this.getItemQuantity(item.id) + 1
				})
			},
			
			// 减少数量
			decreaseQuantity(item) {
				const currentQuantity = this.getItemQuantity(item.id)
				if (currentQuantity > 1) {
					this.updateQuantity({
						id: item.id,
						quantity: currentQuantity - 1
					})
				} else if (currentQuantity === 1) {
					// 如果数量为1，则从购物车中移除
					this.removeFromCart({
						id: item.id
					})
				}
			},
			
			// 去购物
			goShopping() {
				uni.switchTab({
					url: '/pages/index/index'
				})
			}
		}
	}
</script>

<style>
.favorite-container {
	background-color: #f5f5f5;
	min-height: 100vh;
}

.content {
	padding-bottom: 20rpx;
}

/* 收藏列表 */
.favorite-list {
	padding: 0 20rpx;
}

.favorite-item {
	display: flex;
	background-color: #fff;
	border-radius: 12rpx;
	margin: 20rpx 0;
	padding: 20rpx;
	box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.item-left {
	margin-right: 20rpx;
}

.product-image {
	width: 180rpx;
	height: 180rpx;
	border-radius: 8rpx;
}

.item-middle {
	flex: 1;
	display: flex;
	flex-direction: column;
	justify-content: space-between;
}

.product-info {
	flex: 1;
}

.product-name {
	font-size: 30rpx;
	color: #333;
	font-weight: 500;
	margin-bottom: 10rpx;
	display: -webkit-box;
	-webkit-box-orient: vertical;
	-webkit-line-clamp: 2;
	overflow: hidden;
}

.price-section {
	margin: 10rpx 0;
}

.product-original-price {
	font-size: 24rpx;
	color: #999;
	text-decoration: line-through;
	margin-right: 10rpx;
}

.product-discount-price {
	font-size: 32rpx;
	color: #e93b3d;
	font-weight: 500;
}

.product-sold {
	font-size: 24rpx;
	color: #999;
}

.button-section {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-top: 10rpx;
}

.btn-unfavorite {
	font-size: 26rpx;
	color: #999;
}

.add-cart-btn {
	background-color: #e93b3d;
	color: white;
	border-radius: 30rpx;
	padding: 8rpx 20rpx;
	font-size: 26rpx;
}

.quantity-control {
	display: flex;
	align-items: center;
}

.quantity-btn {
	width: 50rpx;
	height: 50rpx;
	background-color: #f5f5f5;
	border-radius: 25rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	font-size: 28rpx;
	color: #333;
}

.quantity-num {
	min-width: 50rpx;
	height: 50rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	font-size: 26rpx;
	color: #333;
	margin: 0 10rpx;
}

/* 空状态 */
.empty-favorite {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	padding: 200rpx 0;
}

.empty-image {
	width: 200rpx;
	height: 200rpx;
	margin-bottom: 30rpx;
}

.empty-text {
	font-size: 30rpx;
	color: #999;
	margin-bottom: 30rpx;
}

.btn-go-shopping {
	width: 200rpx;
	height: 80rpx;
	line-height: 80rpx;
	background-color: #e93b3d;
	color: white;
	border-radius: 10rpx;
	font-size: 28rpx;
}
</style>